package com.huterox.todoscheduler.common.impl;


import com.huterox.todoscheduler.config.Configuration;
import com.huterox.todoscheduler.common.ConfigurationParse;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;

/**
 * 解析配置文件,这里目前还是解析这properties的文件
 * */

public class PropertiesConfigurationParse implements ConfigurationParse, Serializable {

    private final Properties properties = new Properties();

    private void doLoadConfig(String contextproperties) {
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(contextproperties);
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void parseConfig() {
        //加载解析properties配置文件
        this.doLoadConfig(Configuration.configFileName);
        //解析完毕之后，获取对应的属性，然后把解析结果，放在Configuration当中
        String scanPackage = properties.getProperty("scanPackage");
        int schedulerSeconds = Integer.parseInt(properties.getProperty("schedulerSeconds"));

        Configuration.scanPackage = scanPackage;
        Configuration.schedulerSeconds = schedulerSeconds;
        Configuration.corePoolSize = Integer.parseInt(properties.getProperty("corePoolSize"));
        Configuration.maximumPoolSize = Integer.parseInt(properties.getProperty("maximumPoolSize"));
        Configuration.keepAliveTime = Integer.parseInt(properties.getProperty("keepAliveTime"));
        Configuration.WaitingRepairTime = Integer.parseInt(properties.getProperty("WaitingRepairTime"));
        Configuration.ExTimes = Integer.parseInt(properties.getProperty("ExTimes"));
    }
}
